package com.ego.item.controller;

import com.ego.common.utils.PageResult;
import com.ego.item.pojo.Brand;
import com.ego.item.service.BrandService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {


    @Autowired
    private BrandService brandService;


    @GetMapping("/page")
    public ResponseEntity<PageResult<Brand>> listPage(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "descending", defaultValue = "false") Boolean descending,
            @RequestParam(value = "key", required = false) String key
    ) {
        PageResult<Brand> pageResult = brandService.getPageResult(pageNo, pageSize, sortBy, descending, key);

        if (pageResult.getItems() == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } else {
            return ResponseEntity.ok(pageResult);
        }

    }

    /**
     * 新增品牌
     *
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        brandService.addBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        brandService.update(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping
    public ResponseEntity<Void> deleteById(@RequestParam(value = "id") Long id) {
        brandService.deleteById(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> getListByCid(@PathVariable(value = "cid") Long cid) {
        List<Brand> list = brandService.getListByCid(cid);
        if (list != null && list.size() != 0) {
            return ResponseEntity.ok(list);
        } else {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
    }

    @GetMapping("/id")
    public ResponseEntity<Brand> queryBrandById(@RequestParam("id") Long id) {
        Brand brand = this.brandService.queryBrandById(id);
        if (brand == null) {
            new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(brand);
    }

    @GetMapping("list")
    public ResponseEntity<List<Brand>> queryListByIds(@RequestParam("ids") List<Long> ids){
        List<Brand> list = this.brandService.queryBrandListByBrandIds(ids);
        if (list == null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(list);
    }

}
